import {action, observable} from "mobx";
import {Bubble, Tools} from "weaver-mobile";
import {ListViewPage, SelectPage} from "weaver-mobile-page";
import {
    programAccessItemApi,
    programItemDetailApi,
    programItemListApi,
    programItemOperationApi
} from "../../api/performance/programItemApi";

const getLabel = Tools.getLabel;

/**
 * 考核指标模板
 * jiansong 2020/3/4
 */
export default class ProgramItemStore {

    @observable list = new ListViewPage.Store({route: "/performance/programItem"});
    @observable accessItemList = [];
    @observable selectPage = {};
    @observable loading = true;
    @observable canadd = false; //是否能新增
    @observable canedit = false; //是否能编辑

    dataKey = ""; //获取列表数据key

    initStore = (path) => {
        this.selectPage = new SelectPage.Store({route: path + "/detail/target"});
    }

    //查询条件
    @observable queryParams = {
        name: "",//模板名称
    };

    @observable form = { //考核项编辑form
        id: '',
        name: '', //指标模板名称
        description: '', //指标模板说明
        assessDetailData: [], //考核指标项数组
    };

    //获取详情
    getItemDetail = (id) => {
        this.loading=true;
        programItemDetailApi({id: id}).then(action(res => {
            if (res.api_status) {
                this.form.id = res.id;
                this.form.name = res.name;
                this.form.description = res.description;
                this.form.assessDetailData = res.item;
                this.canedit = res.canedit
            } else {
                Bubble.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
            this.loading=false;
        }));
    }

    @action("清空表单")
    clearForm = () => {
        this.form = {
            id: '',
            name: '',
            description: '',
            assessDetailData: [],
        };
    }

    @action("表单值改变")
    onFormChange = (params) => {
        for (let key in params) {
            this.form[key] = params[key];
        }
    }

    @action("搜索条件改变")
    onSearchChange = (params) => {
        for (let key in params) {
            this.queryParams[key] = params[key];
        }
    }

    @action("初始化操作")
    doInit = () => {
        this.queryParams = {
            name: "",
        }
        this.getTableDatas();
    }

    @action("获取考核指标列表")
    getProgramAccessItemList = () => {
        programAccessItemApi().then(action(res => {
            if (res.api_status) {
                this.accessItemList = res.accessItemList;
            } else {
                Bubble.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
        }));
    }

    @action("更改数据")
    setState = (params = {}) => {
        Object.keys(params).forEach((key) => {
            this[key] = params[key];
        });
    }

    @action("渲染table数据")
    getTableDatas = () => {
        programItemListApi(this.queryParams).then(action(res => {
            if (res.api_status) {
                this.list.clearData();
                const {datas: dataKey} = res;
                this.dataKey = dataKey;
                this.list.getData({dataKey});
            } else {
                Bubble.error(res.msg || getLabel(508818, "接口调用失败！"))
            }
        }));
    };

    @action("操作数据")
    operation = (updateListData) => {
        this.form.assessDetailData = JSON.stringify(this.form.assessDetailData);
        programItemOperationApi(this.form).then((res) => {
            if (res.api_status) {
                if (updateListData && typeof updateListData == "function") {
                    updateListData(this.form)
                }
                this.clearForm();
                Bubble.success(getLabel('30700', '操作成功'));
            } else {
                Bubble.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
        })
    }

    @action('更新列表数据数据')
    updateListData = (params) => {
        const {data, updateDataSource, setState} = this.list;
        let index = data.findIndex(item => item.id == params.id);
        if (index != -1) {
            Tools.callApi({
                url: '/api/ec/dev/table/datas',
                method: 'POST',
                params: {dataKey: this.dataKey, current: index + 1, pageSize: 1},
            }).then(res => {
                if (res.datas && res.datas[0]) {
                    data[index] = res.datas[0];
                    setState({data});
                    updateDataSource();
                }
            })
        } else {
            this.getTableDatas();
        }
    }

}